// 滚动到顶部
function returnTop() {
  //记录当前执行动画的标识
  let animationStepNumber;
  function exeucteAnimationByStep() {
    //当前页面的滚动高度
    let currentScrollTop = document.documentElement.scrollTop || document.body.scrollTop;
    if (currentScrollTop >= 4) {
      animationStepNumber = window.requestAnimationFrame(exeucteAnimationByStep);
      let scrollLocationChanging = currentScrollTop / 9;
      scrollLocationChanging = scrollLocationChanging > 1 ? scrollLocationChanging : 1;
      let newScrollTop = currentScrollTop - scrollLocationChanging;
      window.scrollTo(0, newScrollTop);
    } else {
      window.cancelAnimationFrame(animationStepNumber);
      window.scrollTo(0, 0);
    }
  }
  animationStepNumber = window.requestAnimationFrame(exeucteAnimationByStep);
}

function scrollToTop() {
  let exerciseContainerRoot = (this.$refs.move);
  let scrollIntoViewOptions = {
    block: 'start',//[可选] "start"（顶部），"center"（居中），"end"（尾部） 或"nearest"。默认为"center"。
    behavior: 'smooth',//[可选]定义过渡动画。"auto","instant"或"smooth"。默认为"auto"。
    // inline：[可选] "start"，"center"，"end"或"nearest"。默认为"nearest"。
  };
  exerciseContainerRoot.scrollIntoView(scrollIntoViewOptions);
}

// 获取dom上下左右距离视口的距离和宽高
function isInViewPort(element) {
  const viewWidth = window.innerWidth || document.documentElement.clientWidth;
  const viewHeight = window.innerHeight || document.documentElement.clientHeight;
  const {
    top,
    right,
    bottom,
    left,
  } = element.getBoundingClientRect();

  console.log(top, right, bottom, left, element.getBoundingClientRect());
  return (
    top >= 0 &&
    left >= 0 &&
    right <= viewWidth &&
    bottom <= viewHeight
  )
}

// 滚动到底部
function scrollToPageBottom(){
  let animationStepNumber;
  function exeucteAnimationByStep() {
    let currentScrollTop = document.documentElement.scrollTop || document.body.scrollTop;
    let totalHeight = (document.documentElement.scrollHeight || document.body.scrollHeight) - window.innerHeight;
    //剩余的高度
    let scrollingHeight = totalHeight - currentScrollTop;
    if (scrollingHeight >= 4) {
      animationStepNumber = window.requestAnimationFrame(exeucteAnimationByStep);
      //滚动变量
      let scrollChangedHeight = scrollingHeight / 9;
      scrollChangedHeight = scrollChangedHeight > 1 ? scrollChangedHeight : 1;
      window.scrollTo(0, currentScrollTop + scrollChangedHeight);
    } else {
      window.cancelAnimationFrame(animationStepNumber);
      window.scrollTo(0, totalHeight);
    }
  }
  animationStepNumber = window.requestAnimationFrame(exeucteAnimationByStep);
}
